package com.leonyr.smartipaddemo.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.leonyr.smartipaddemo.InitApplication;
import com.leonyr.smartipaddemo.R;
import com.leonyr.smartipaddemo.bean.PicItem;

import java.util.List;

/**
 * 适配图标显示
 * Date： 2020/6/8 0008
 * Time： 14:57
 * Created by xtqb.
 */
public class PicBaseAdapter extends BaseAdapter {
    private List<PicItem> picList;
    private ViewHolder holder;

    public PicBaseAdapter(List<PicItem> picList) {
        this.picList = picList;
    }

    @Override
    public int getCount() {
        if (picList == null || picList.size() == 0) return 0;
        return picList.size();
    }

    @Override
    public Object getItem(int position) {
        return picList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(InitApplication.getContext()).inflate(R.layout.item_grid_view, null);
            convertView.setBackgroundColor(InitApplication.getContext().getResources().getColor(R.color.black));
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.iv_image.setImageBitmap(picList.get(position).getBitmap());
        holder.iv_state.setVisibility(picList.get(position).isVisible() ? View.VISIBLE : View.GONE);
        if (picList.get(position).isVisible()) {
            holder.iv_state.setImageResource(picList.get(position).isSelected() ? R.drawable.icon_slected : R.drawable.icon_unselected);
        }
        return convertView;
    }


    private class ViewHolder {
        private ImageView iv_image;
        private ImageView iv_state;

        private ViewHolder(View view) {
            iv_state = view.findViewById(R.id.iv_state);
            iv_image = view.findViewById(R.id.iv_image);
        }
    }
}
